<?php
namespace App\DBs\Reward;

use Illuminate\Support\Facades\DB;

class SignDB extends BaseDB
{
    /**
     * 用户签到
     * uid 用户UID
     * uname 用户名称
     * lon 经度
     * lat 纬度
     */

    protected $table = 'reward_sign';

    public function __construct()
    {
    }

    /**
     * 列表
     */
    public function getIndex($uid)
    {
        $query = DB::table($this->table);
        if ($uid) { $query = $query->where('uid',$uid); }
        $models = $query->orderBy('id','desc')
            ->skip(0)->take(10)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $items[] = [
                'uid' => $model['uid'],
                'uname' => $model['uname'],
                'lon' => $model['lon'],
                'lat' => $model['lat'],
                'createTime' => $model['createTime'],
            ];
        }
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$items);
    }

    /**
     * 获取一个记录，
     * @param $uid 用户UID
     */
    public function getOne($uid)
    {
        $start = strtotime(date('YmdH',time()).'0001');
        $end = strtotime(date('YmdH',time()).'5959');
        $query = DB::table($this->table);
        if ($uid) { $query = $query->where('uid',$uid); }
        $models = $query->orderBy('id','desc')
            ->where('created_at','>=',$start)
            ->where('created_at','<=',$end)
            ->skip(0)->take(1)
            ->get();
        if (!count($models)) {
            return array('code'=>'-1', 'msg'=>'没有数据');
        }
        $model = $this->jsonToArr($models[0]);
        $items = [
            'uid' => $model['uid'],
            'lon' => $model['lon'],
            'lat' => $model['lat'],
            'createTime' => $model['createTime'],
        ];
        return array('code'=>'0', 'msg'=>'获取成功', 'data'=>$items);
    }

    /**
     * 新增
     */
    public function setAdd($uid,$uname,$lon,$lat)
    {
        if (!$uid || !$lon || !$lat) {
            return array('code'=>'-1', 'msg'=>'参数错误');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $created_at = time();
            $createTime = date('Y-m-d H:i:s',$created_at);
            $lastId = DB::table($this->table)
                ->insertGetId([
                    'uid' => $uid,
                    'uname' => $uname,
                    'lon' => $lon,
                    'lat' => $lat,
                    'created_at' => $created_at,
                    'createTime' => $createTime,
                ]);
            $dataArr = array(
                'lastId' => $lastId,
                'createTime' => $createTime,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }
}